package tables

import (
	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/form"
)

func GetAppsTable(ctx *context.Context) table.Table {
	tableName := "storage_apps"
	title := "Apps"

	app := table.NewDefaultTable(table.DefaultConfigWithDriver("mysql")) // .DefaultConfig().SetPrimaryKey("uuid", db.Varchar)


	thisInfo := app.GetInfo()

	// thisInfo.AddLimitFilter(limit int)
	// 	.AddTrimSpaceFilter()
	// 	.AddSubstrFilter(start int, end int)
	// 	.AddToTitleFilter()
	// 	.AddToUpperFilter()
	// 	.AddToLowerFilter()
	// 	.AddXssFilter()
	// 	.AddXssJsFilter()

	// thisInfo.HideExportButton()
	// thisInfo.HideRowSelector()
	// thisInfo.HideFilterArea()
	// thisInfo.HideNewButton()
	// thisInfo.HideEditButton()
	// thisInfo.HideDeleteButton()
	// thisInfo.HideDetailButton()


	// thisInfo.Where("type", "=", 0)
	// thisInfo.ExportValue() // 未经显示处理的原生值
	// thisInfo.SetTableFixed()
	thisInfo.SetFilterFormLayout(form.LayoutThreeCol)
	thisInfo.SetFilterFormHeadWidth(3)
	thisInfo.SetFilterFormInputWidth(9)
	// thisInfo.SetQueryFilterFn(func(param parameter.Parameters, conn db.Connection) (ids []string, stopQuery bool) {
	// })
	// thisInfo.SetSortAsc()
	// thisInfo.SetSortDesc()
	// thisInfo.SetSortField("created_at")
	// thisInfo.AddButton("Today", icon.Save, action.PopUp("/admin/data/analyze", "TodayStatistic"))
	// thisInfo.AddSelectBox("gender", types.FieldOptions{
	// 	{Value: "", Text: ""},
	// 	{Value: "0", Text: "men"},
	// 	{Value: "1", Text: "women"},
	// }, action.FieldFilter("gender"))
	// thisInfo.AddColumnButtons(head string, buttons ...Button)
	// thisInfo.AddColumn(head string, fn FieldFilterFn)

	// thisInfo.AddField("Role Name", "role_name", db.Varchar).FieldJoin(types.Join{
	// 	Table: "role",
	// 	Field: "id",
	// 	JoinField: "user_id",
	// })
	// thisInfo.AddField("Role Name", "role_name", db.Varchar).FieldJoin(types.Join{
	// 	Table: "role_users",
	// 	Field: "id",
	// 	JoinField: "user_id",
	// }).FieldJoin(types.Join{
	// 	Table: "roles",
	// 	Field: "role_id",
	// 	JoinField: "id",
	// 	BaseTable: "role_users",
	// })

	thisInfo.AddField("ID", "id", db.Int).
		FieldFilterable().
		FieldSortable().
		FieldFixed().
		// FieldHide().
		FieldWidth(100)

	// FieldTrimSpace().
	// FieldLimit(10).
	// FieldSubstr(0, 3).
	// FieldToTitle().
	// FieldToUpper().
	// FieldToLower()

	thisInfo.AddField("Slug", "slug", db.Varchar).
		FieldFilterable().FieldSortable()
	thisInfo.AddField("Title", "title", db.Varchar).
		FieldFilterable().FieldSortable().
		FieldFilterable(types.FilterType{Operator: types.FilterOperatorLike})

	thisInfo.AddField("Description", "description", db.Text).
		FieldFilterable()

	thisInfo.AddField("Sort", "sort", db.Tinyint).
		FieldFilterable().FieldSortable()
	thisInfo.AddField("Created by", "created_by", db.Tinyint).
		FieldFilterable().FieldSortable().
		FieldDisplay(func(model types.FieldModel) interface{} {
			return "<span class='label'>" +  model.Value + "</span>"
			// return model.Row["first_name"].(string)
		}).FieldHide()
	thisInfo.AddField("Created at", "created_at", db.Datetime).
		FieldFilterable().FieldSortable()
	thisInfo.AddField("Updated at", "updated_at", db.Datetime).
		FieldFilterable().FieldSortable()


	// thisInfo.AddField("CreatedAt", "created_at", db.Timestamp).
	// 	FieldFilterable(types.FilterType{FormType: form.DatetimeRange})
	// 	FieldFilterable(types.FilterType{Operator: types.FilterOperatorLike}).
	// 	FieldFilterProcess(func(s string) string {
	// 		return strings.TrimSpace(s)
	// 	})

	// thisInfo.AddActionButton("Opration", action.Jump("https://google.com"))

	// thisInfo.AddField("显示状态", "show_status", db.Tinyint).
	// 	FieldEditAble(table.Switch).FieldEditOptions(types.FieldOptions{
	// 		{Value: "1", Text: "允许"},
	// 		{Value: "2", Text: "禁止"},
	// 	}).
	// 	FieldFilterable(types.FilterType{FormType: form.SelectSingle}).FieldFilterOptions(types.FieldOptions{
	// 		{Value: "1", Text: "允许"},
	// 		{Value: "2", Text: "禁止"},
	// 	}).FieldFilterOptionExt(map[string]interface{}{"allowClear": true})


	// adminPlugin := admin.NewAdmin()
	// adminPlugin.AddDisplayFilterTrimSpace()
	// 	.AddDisplayFilterLimit(limit int)
	// 	.AddDisplayFilterSubstr(start int, end int)
	// 	.AddDisplayFilterToTitle()
	// 	.AddDisplayFilterToUpper()
	// 	.AddDisplayFilterToLower()
	// 	.AddDisplayFilterXssFilter()
	// 	.AddDisplayFilterXssJsFilter()


	thisInfo.SetTable(tableName).
		SetTitle(title).SetDescription("List") // .SetAction(template.HTML(`<a href="http://google.com"><i class="fa fa-google"></i></a>`))


	thisDetail := app.GetDetail()
	// thisDetail := app.GetDetailFromInfo()

	thisDetail.AddField("ID", "id", db.Int)

	thisDetail.AddField("Slug", "slug", db.Varchar)
	thisDetail.AddField("Title", "title", db.Varchar)
	thisDetail.AddField("Config", "config", db.Varchar)
	thisDetail.AddField("Description", "description", db.Varchar)

	thisDetail.AddField("Sort", "sort", db.Int)
	thisDetail.AddField("Created by", "created_by", db.Varchar)
	thisDetail.AddField("Created at", "created_at", db.Varchar)
	thisDetail.AddField("Updated at", "updated_at", db.Varchar)

	thisDetail.SetTable(tableName).
		SetTitle(title).SetDescription("Detail")


	thisForm := app.GetForm()

	// thisForm.AddLimitFilter(limit int)
	// 	.AddTrimSpaceFilter()
	// 	.AddSubstrFilter(start int, end int)
	// 	.AddToTitleFilter()
	// 	.AddToUpperFilter()
	// 	.AddToLowerFilter()
	// 	.AddXssFilter()
	// 	.AddXssJsFilter()

	thisForm.AddField("ID", "id", db.Int, form.Default).
		FieldDisableWhenCreate()

	thisForm.AddField("Slug", "slug", db.Varchar, form.Text)
	thisForm.AddField("Title", "title", db.Varchar, form.Text)
	thisForm.AddField("Config", "config", db.Text, form.TextArea)
	thisForm.AddField("Description", "description", db.Text, form.RichText)

	thisForm.AddField("Sort", "sort", db.Tinyint, form.Text)
	thisForm.AddField("Created by", "created_by", db.Int, form.Text)
	thisForm.AddField("Created at", "created_at", db.Datetime, form.Datetime).
		FieldHide().FieldNowWhenInsert()
	thisForm.AddField("Updated at", "updated_at", db.Datetime, form.Datetime).
		FieldHide().FieldNowWhenUpdate()

	thisForm.SetTable(tableName).
		SetTitle(title).SetDescription("Edit")


	return app
}
